import { View } from "../../../mvc/core/View";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ObjCreate } from "../common/ObjCreate";
import { ResulteItem } from "./ResulteItem";
import { Language } from "../../../../extend/language/Language";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { ResFileConst } from "../../resource/ResFileConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { DataEvent } from "../../events/DataEvent";
import { EExpDiceAttribute } from "../../../../Message/Public/EExpDiceAttribute";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { Cache } from "../../cache/Cache";
import { MsgManager } from "../../manager/MsgManager";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { Alert } from "../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../common/alertwins/CheckBoxWin";
import { ImagesConst } from "../../resource/ImagesConst";
import { TweenMax } from "../../../../com/greensock/TweenMax";
import { Circ } from "../../../../com/greensock/easing/Circ";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class PlayDiceResultPanel
    
    export  class PlayDiceResultPanel extends View
    {
        constructor()
        {
            super();
            this._diceArr = [];
            this._picBgArr = [];
            this._diceBmDArr = [];
            this._picArr = [];
            this._stateArr = [];
            this.initView();
            return;
        }

        public set tweenArr(arg1: Array<any>)
        {
            this._tweenArr = arg1;
            return;
        }

        /* internal  */initView(): void
        {
            var loc1=null;
            var loc2=null;
            LoaderHelp.addResCallBack("skillUI.swf", this.onBgLoadedHandler);
            this._stBmbg = ObjCreate.createBitmap("", 0, 0, this);
            this._stBmRuslt = ObjCreate.createBitmap("", 178, 11, this);
            this._resultItem = new ResulteItem();
            addChild(this._resultItem);
            this._resultItem.x = 25;
            this._resultItem.y = 39;
            this._ybWarnTx = ObjCreate.createTextField("", 330, 132, 108, 20, this);
            this._sureBtn = ObjCreate.createGButton(Language.getString(70310), 73, 130, 70, 22, this, "ButtonRedMini");
            this._changeBtn = ObjCreate.createGButton("", 250, 130, 70, 22, this, "ButtonRedMini");
            this.changeBtn();
            this._sureBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClick);
            this._changeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClick);
            this._oneSixDiceBtn = ObjCreate.createGButton(Language.getString(70326), 163, 130, 70, 22, this, "ButtonRedMini");
            this._oneSixDiceBtn.toolTipData = HTMLUtil.addColor(Language.getString(70328), "#ffffff");
            this._oneSixDiceBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClick);
            var loc3=0;
            while (loc3 < 6) 
            {
                loc1 = ObjCreate.createBitmap("", 60 + 52 * loc3, 80, this);
                this._picBgArr.push(loc1);
                loc2 = ObjCreate.createBitmap("", 63 + 52 * loc3, 83, this);
                this._picArr.push(loc2);
                ++loc3;
            }
            LoaderHelp.addResCallBack(ResFileConst.playDice, this.onLoadBmResource);
            this._timer = new Object /* flash.utils.Timer */(10);
            this._timer.addEventListener(flash.events.TimerEvent.TIMER, this.onTimer);
            Dispatcher.addEventListener(EventName.PlayDiceResultTimerOn, this.onResultTimerOn);
            return;
        }

        /* internal  */onResultTimerOn(arg1: DataEvent): void
        {
            this._timer.start();
            return;
        }

        protected onTimer(arg1: Object /* flash.events.TimerEvent */): void
        {
            var loc2=false;
            var loc4=0;
            var loc5=0;
            var loc1=this._stateArr.length;
            if (this._stateArr.length == 0) 
            {
                loc5 = 0;
                while (loc5 < this._diceArr.length) 
                {
                    loc4 = this._diceArr[loc5];
                    this._stateArr.push(loc4 != EExpDiceAttribute._EExpDiceAttributeFu ? false : true);
                    ++loc5;
                }
            }
            var loc3=0;
            while (loc3 < loc1) 
            {
                loc2 = this._stateArr[loc3];
                if (loc2) 
                {
                    this._picArr[loc3].bitmapData = this._diceBmDArr[0];
                }
                else 
                {
                    this._picArr[loc3].bitmapData = this.randomBD();
                }
                ++loc3;
            }
            return;
        }

        /* internal  */randomBD(): Object /* flash.display.BitmapData */
        {
            var loc1=Math.floor(Math.random() * this._picArr.length);
            var loc2=this._diceBmDArr[loc1];
            return loc2;
        }

        /* internal  */onBgLoadedHandler(): void
        {
            var loc1=0;
            while (loc1 < this._picBgArr.length) 
            {
                this._picBgArr[loc1].bitmapData = GlobalClass.getBitmapData("skillBg");
                ++loc1;
            }
            return;
        }

        protected onBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=false;
            var loc2=false;
            if (this.isRunning) 
            {
                return;
            }
            var loc3=arg1.target;
            switch (loc3) 
            {
                case this._sureBtn:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DiceGoThrow, {"type": 2, "bool": false}));
                    hide();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.CloseNpcRewardTips));
                    break;
                }
                case this._changeBtn:
                {
                    if (Cache.instance.playDice.sExpDice.toModifyCount >= 10) 
                    {
                        if (Cache.instance.role.money.gold < int(Cache.instance.playDice.getYb())) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(70316));
                            return;
                        }
                        loc1 = !PulseSharedObject.isTodayNotTips("PlayDiceCost");
                        if (loc1) 
                        {
                            Alert.alertWinRenderer = CheckBoxWin;
                            Alert.show(Language.getStringByParam(70324, Cache.instance.playDice.getYb()), null, Alert.OK | Alert.CANCEL, null, this.onBtnClickHandler);
                        }
                        else 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.DiceGoThrow, {"type": 1, "bool": false}));
                        }
                    }
                    else 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.DiceGoThrow, {"type": 1, "bool": false}));
                    }
                    break;
                }
                case this._oneSixDiceBtn:
                {
                    if (Cache.instance.playDice.fullFreeChange && Cache.instance.role.money.gold < parseInt(Cache.instance.playDice.getYb())) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(70316));
                        return;
                    }
                    loc2 = !PulseSharedObject.isTodayNotTips("oneSixDice");
                    if (loc2) 
                    {
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.show(Language.getStringByParam(70327, Cache.instance.playDice.getFreeChangeCount(), Cache.instance.role.money.gold), null, Alert.OK | Alert.CANCEL, null, this.onOneSixDiceBtnClickHandler);
                    }
                    else 
                    {
                        this.goOneSixDice();
                    }
                    break;
                }
            }
            this.changeBtn();
            return;
        }

        /* internal  */onOneSixDiceBtnClickHandler(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                Alert.resetToDefault();
                this.goOneSixDice();
            }
            if (arg2) 
            {
                PulseSharedObject.save("oneSixDice", true);
            }
            return;
        }

        /* internal  */goOneSixDice(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.DiceGoThrow, {"type": 1, "bool": true}));
            return;
        }

        /* internal  */onBtnClickHandler(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                Alert.resetToDefault();
                Dispatcher.dispatchEvent(new DataEvent(EventName.DiceGoThrow, {"type": 1, "bool": false}));
            }
            if (arg2) 
            {
                PulseSharedObject.save("PlayDiceCost", true);
            }
            return;
        }

        /* internal  */onLoadBmResource(): void
        {
            var loc1=0;
            this._loadedFlag = true;
            this._stBmbg.bitmapData = GlobalClass.getBitmapData(ImagesConst.DiceResultBg);
            this._stBmRuslt.bitmapData = GlobalClass.getBitmapData(ImagesConst.DiceGambleResult);
            this._diceBmDArr.push(GlobalClass.getBitmapData(ImagesConst.DiceFu), GlobalClass.getBitmapData(ImagesConst.DiceMoney), GlobalClass.getBitmapData(ImagesConst.DiceYb), GlobalClass.getBitmapData(ImagesConst.DiceHulu), GlobalClass.getBitmapData(ImagesConst.DicePack), GlobalClass.getBitmapData(ImagesConst.DiceQuest));
            if (Cache.instance.playDice.type == 0) 
            {
                loc1 = 0;
                while (loc1 < this._picArr.length) 
                {
                    this._picArr[loc1].bitmapData = this._diceBmDArr[(this._diceArr[loc1] - 1)];
                    ++loc1;
                }
            }
            return;
        }

        public setDice(arg1: Array<any>, arg2: int): void
        {
            var loc2=0;
            var loc3=false;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            var loc7=0;
            var loc8=0;
            var loc9=0;
            var loc10=0;
            var loc11=0;
            var loc12=0;
            this._tweenArr = [];
            var loc1=arg1.concat();
            loc1.sort(Array.NUMERIC);
            if (this._diceArr.length > 0) 
            {
                if (Cache.instance.playDice.type != 0) 
                {
                    loc6 = 0;
                    while (loc6 < arg1.length) 
                    {
                        loc7 = loc1[loc6];
                        loc8 = this._diceArr[loc6];
                        if (loc7 == EExpDiceAttribute._EExpDiceAttributeFu && !(loc8 === EExpDiceAttribute._EExpDiceAttributeFu)) 
                        {
                            loc3 = true;
                        }
                        else 
                        {
                            loc3 = false;
                        }
                        this._tweenArr.push(loc3);
                        ++loc6;
                    }
                }
                else 
                {
                    loc4 = 0;
                    while (loc4 < arg1.length) 
                    {
                        loc5 = loc1[loc4];
                        this._tweenArr.push(loc5 != EExpDiceAttribute._EExpDiceAttributeFu ? false : true);
                        ++loc4;
                    }
                }
            }
            else 
            {
                loc9 = 0;
                while (loc9 < arg1.length) 
                {
                    loc10 = loc1[loc9];
                    this._tweenArr.push(loc10 != EExpDiceAttribute._EExpDiceAttributeFu ? false : true);
                    ++loc9;
                }
            }
            this._diceArr = arg1.concat();
            this._diceArr.sort(Array.NUMERIC);
            this._resultItem.upDate(arg2);
            this._stateArr = [];
            if (Cache.instance.playDice.type != 0) 
            {
                if (Cache.instance.playDice.type == 1) 
                {
                    loc12 = 0;
                    while (loc12 < this._diceArr.length) 
                    {
                        loc2 = this._diceArr[loc12];
                        this._stateArr.push(loc2 != EExpDiceAttribute._EExpDiceAttributeFu ? false : true);
                        ++loc12;
                    }
                }
            }
            else 
            {
                loc11 = 0;
                while (loc11 < this._picArr.length) 
                {
                    this._picArr[loc11].bitmapData = this._diceBmDArr[(this._diceArr[loc11] - 1)];
                    ++loc11;
                }
            }
            if (Cache.instance.playDice.getFuNum() != EExpDiceAttribute._EExpDiceAttributeWhat) 
            {
                this._sureBtn.x = 73;
                this._changeBtn.visible = true;
                this._oneSixDiceBtn.visible = true;
            }
            else 
            {
                this._sureBtn.x = 183;
                this._changeBtn.visible = false;
                this._oneSixDiceBtn.visible = false;
            }
            this.changeBtn();
            return;
        }

        /* internal  */changeBtn(): void
        {
            if (Cache.instance.playDice.fullFreeChange) 
            {
                this._changeBtn.label = Language.getString(70312);
                this._changeBtn.toolTipData = HTMLUtil.addColor(Language.getStringByParam(70323, Cache.instance.playDice.getYb()), "#ffffff");
                if (this._changeBtn.visible) 
                {
                    this._ybWarnTx.htmlText = Language.getStringByParam(70325, Cache.instance.playDice.getYb());
                }
                else 
                {
                    this._ybWarnTx.htmlText = "";
                }
            }
            else 
            {
                this._changeBtn.label = Language.getString(70311);
                this._changeBtn.toolTipData = HTMLUtil.addColor(Language.getStringByParam(70322, Cache.instance.playDice.getFreeChangeCount()), "#ffffff");
                this._ybWarnTx.htmlText = "";
            }
            return;
        }

        public stop(): void
        {
            this._timer.stop();
            var loc1=0;
            while (loc1 < this._picArr.length) 
            {
                this._picArr[loc1].bitmapData = this._diceBmDArr[(this._diceArr[loc1] - 1)];
                ++loc1;
            }
            this.tweenFu(this.tweenArr);
            return;
        }

        /* internal  */tweenFu(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=0;
            while (loc4 < arg1.length) 
            {
                if (arg1[loc4] == true) 
                {
                    loc1 = new Object /* flash.display.Bitmap */();
                    if (this._loadedFlag) 
                    {
                        loc1.bitmapData = GlobalClass.getBitmapData(ImagesConst.DiceFu);
                    }
                    loc1.x = 222;
                    loc1.y = 254;
                    loc1.scaleX = 0.5;
                    loc1.scaleY = 0.5;
                    this.addChildAt(loc1, this.numChildren);
                    loc2 = 63 + loc4 * 52;
                    loc3 = 83;
                    TweenMax.to(loc1, 0.8, {"x": loc2, "y": loc3, "scaleX": 1, "scaleY": 1, "ease": Circ.easeOut, "onComplete": this.onPlayEffectCompleteHandler, "onCompleteParams":[loc1]});
                }
                ++loc4;
            }
            return;
        }

        /* internal  */onPlayEffectCompleteHandler(arg1: Object /* flash.display.Bitmap */): void
        {
            if (arg1.parent) 
            {
                arg1.parent.removeChild(arg1);
            }
            return;
        }

        public get tweenArr(): Array<any>
        {
            return this._tweenArr;
        }

        /* internal */ /* var */_stBmbg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_stBmRuslt: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_sureBtn: GButton;

        /* internal */ /* var */_changeBtn: GButton;

        /* internal */ /* var */_diceArr: Array<any>;

        /* internal */ /* var */_picBgArr: Array<any>;

        /* internal */ /* var */_diceBmDArr: Array<any>;

        /* internal */ /* var */_picArr: Array<any>;

        /* internal */ /* var */_timer: Object /* flash.utils.Timer */;

        /* internal */ /* var */_stateArr: Array<any>;

        /* internal */ /* var */_resultItem: ResulteItem;

        /* internal */ /* var */_ybWarnTx: Object /* flash.text.TextField */;

        public /* var */isRunning: Boolean=false;

        public /* var */_oneSixDiceBtn: GButton;

        /* internal */ /* var */_tweenArr: Array<any>;

        /* internal */ /* var */_loadedFlag: Boolean;
    }
